package com.bluewave.goaltracker;

import java.util.Date;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.bluewave.goaltracker.utils.Goal;

public class AlarmBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		AlarmManager alarmManager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);

		final Context mContext = context.getApplicationContext();

		SharedPreferences pres = PreferenceManager
				.getDefaultSharedPreferences(mContext);

		int alert_type =0 ; 
		if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
			alert_type = Goal.MONTHLY;
		}else{
			alert_type = intent.getIntExtra("alert_type", 200);
		}
		if (alert_type == Goal.MONTHLY) {

			Intent cIntent = new Intent(
			"com.bluewave.DAILY_NOTIFICATION_BROADCAST_RECEIVER");

			String strTime = pres.getString("alert_time", "12:00");
			boolean alert_enable = pres.getBoolean("daily_alert", true);

			String strHour = strTime.split(":")[0];
			String strMinutes = strTime.split(":")[1];

			Date date = new Date();

			date.setHours(Integer.parseInt(strHour));
			date.setMinutes(Integer.parseInt(strMinutes));

			cIntent.putExtra("notification_type", Goal.MONTHLY);

			PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
					0, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

			Log.i("AlarmBroadcastReceiver", "Cancelling Daily Alert");
			alarmManager.cancel(pendingIntent);

			if(alert_enable){
				while (date.getTime() < new Date().getTime()) {

					date.setTime(date.getTime() + AlarmManager.INTERVAL_DAY);

				}

				Log.i("AlarmBroadcastReceiver", "Setting Daily Alert Alarm");
				alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTime(),
						AlarmManager.INTERVAL_DAY, pendingIntent);
			}

		} else if (alert_type == Goal.QUARTERLY) {

			Intent cIntent = new Intent(
			"com.bluewave.QUARTERLY_NOTIFICATION_BROADCAST_RECEIVER");
			
			long quarterly_alert = pres.getLong("quarterly_alert", 0);

			cIntent.putExtra("notification_type", Goal.QUARTERLY);

			PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
					0, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

			alarmManager.cancel(pendingIntent);

			while (quarterly_alert < new Date().getTime()) {

				quarterly_alert = quarterly_alert
						+ (AlarmManager.INTERVAL_DAY * 30 * 3);
			}

			alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, quarterly_alert,
					AlarmManager.INTERVAL_DAY, pendingIntent);

		}
		else if (alert_type == Goal.NOT_USED_ALERT) {

			Intent cIntent = new Intent(
			"com.bluewave.NOT_USED_NOTIFICATION_BROADCAST_RECEIVER");

			String strTime = pres.getString("not_used_alert_time", "12:00");
			boolean alert_enable = pres.getBoolean("not_used_alert", true);

			String strHour = strTime.split(":")[0];
			String strMinutes = strTime.split(":")[1];

			Date date = new Date();

			date.setHours(Integer.parseInt(strHour));
			date.setMinutes(Integer.parseInt(strMinutes));

			cIntent.putExtra("notification_type", Goal.NOT_USED_ALERT);

			PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
					0, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

			Log.i("AlarmBroadcastReceiver", "Cancelling Not used Alert");
			alarmManager.cancel(pendingIntent);

			if(alert_enable){
				while (date.getTime() < new Date().getTime()) {

					date.setTime(date.getTime() + 30*AlarmManager.INTERVAL_DAY);

				}

				Log.i("AlarmBroadcastReceiver", "Setting Not Used Alert Alarm");
				alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTime(),
						30*AlarmManager.INTERVAL_DAY, pendingIntent);
			}

		} 
		else {

			Intent cIntent = new Intent(
			"com.bluewave.DAILY_NOTIFICATION_BROADCAST_RECEIVER");
			
			String strTime = pres.getString("alert_time", "12:00");

			String strHour = strTime.split(":")[0];
			String strMinutes = strTime.split(":")[1];

			Date date = new Date();

			date.setHours(Integer.parseInt(strHour));
			date.setMinutes(Integer.parseInt(strMinutes));

			cIntent.putExtra("notification_type", Goal.MONTHLY);

			PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
					0, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

			alarmManager.cancel(pendingIntent);

			while (date.getTime() < new Date().getTime()) {

				date.setTime(date.getTime() + AlarmManager.INTERVAL_DAY);

			}

			alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTime(),
					AlarmManager.INTERVAL_DAY, pendingIntent);

			long quarterly_alert = pres.getLong("quarterly_alert", 0);

			cIntent.putExtra("notification_type", Goal.QUARTERLY);

			PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,
					0, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

			alarmManager.cancel(pendingIntent2);

			while (quarterly_alert < new Date().getTime()) {

				quarterly_alert = quarterly_alert
						+ (AlarmManager.INTERVAL_DAY * 30 * 3);
			}

			alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, quarterly_alert,
					AlarmManager.INTERVAL_DAY, pendingIntent2);
		}

	}
}
